package com.qms.repast.ui.activity;

import android.content.Intent;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qms.repast.R;
import com.qms.repast.entity.MessageEvent;
import com.qms.repast.entity.resbean.DiscountBean;
import com.qms.repast.ui.adapter.DiscountAdapter;
import com.qms.repast.ui.adapter.OrderConfirmAdapter;
import com.qms.repast.ui.base.BaseActivity;
import com.qms.repast.ui.presenter.OrderConfirmPresenter;
import com.qms.repast.ui.view.IOrderConfirmView;
import com.qms.repast.utils.AppUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author 泅渡者
 * @date 2018/10/22 0022
 * @describe: 订单确认界面
 */
public class OrderConfirmActivity extends BaseActivity<OrderConfirmPresenter> implements IOrderConfirmView {
	@BindView(R.id.iv_back)
	ImageView ivBack;
	@BindView(R.id.tv_title)
	TextView tvTitle;
	@BindView(R.id.recyclerView)
	RecyclerView recyclerView;
	@BindView(R.id.ll_discount)
	LinearLayout llDiscount;
	@BindView(R.id.ll_redEnvelope)
	LinearLayout llRedEnvelope;
	@BindView(R.id.ll_remarks)
	LinearLayout llRemarks;
	@BindView(R.id.tv_totalNum)
	TextView tvTotalNum;
	@BindView(R.id.tv_totalPrice)
	TextView tvTotalPrice;
	@BindView(R.id.tv_commit)
	TextView tvCommit;
	/**
	 * 店铺名称
	 */
	private String shopName;
	/**
	 * 店铺ID
	 */
	private String shopId;
	/**
	 * 用户ID
	 */
	private String userId;


	private List<String> stringList;
	private OrderConfirmAdapter orderConfirmAdapter;

	/**
	 * 折扣券集合
	 */
	private List<DiscountBean> discounts;
	/**
	 * 折扣券列表
	 */
	private RecyclerView rlDiscount;
	/**
	 * 折扣券适配器
	 */
	private DiscountAdapter discountAdapter;
	/**
	 * 折扣券弹窗
	 */
	private BottomSheetDialog bottomSheetDialog;

	private Intent mIntent;

	@Override
	protected int initLayout () {
		return R.layout.activity_order_confirm;
	}

	@Override
	protected void initPresenter (Intent intent) {
		mPresenter = new OrderConfirmPresenter(this, this);
		shopName = intent.getStringExtra("shopName");
		shopId = intent.getStringExtra("shopId");
		userId = intent.getStringExtra("userId");
	}

	@Override
	protected void initView () {
		EventBus.getDefault().register(this);
		tvTitle.setText("订单确认");
		stringList = new ArrayList<>();
		stringList.add("1");
		stringList.add("2");
		stringList.add("3");
		stringList.add("4");
		stringList.add("5");
		orderConfirmAdapter = new OrderConfirmAdapter(AppUtils.getContext());
		recyclerView.setLayoutManager(new LinearLayoutManager(this));
		recyclerView.setAdapter(orderConfirmAdapter);
		recyclerView.setNestedScrollingEnabled(false);
		orderConfirmAdapter.setList(stringList, userId, shopId);
	}

	@Override
	protected void initData () {

	}

	@Subscribe
	public void onEventMainThread (MessageEvent messageEvent) {
		switch (messageEvent.getCode()) {
			default:
				break;
		}
	}


	@OnClick({R.id.iv_back, R.id.ll_discount, R.id.ll_redEnvelope, R.id.tv_commit, R.id.ll_remarks})
	public void onViewClicked (View view) {
		switch (view.getId()) {
			case R.id.iv_back:
				finish();
				break;
			case R.id.ll_discount:
				showOrHidePop();
				break;
			case R.id.ll_redEnvelope:
				showOrHidePop();
				break;
			case R.id.tv_commit:
				break;
			case R.id.ll_remarks:
				mIntent = new Intent(OrderConfirmActivity.this,OrderRemarkActivity.class);
				mIntent.putExtra("shopId",shopId);
				startActivity(mIntent);
				break;
			default:
				break;
		}
	}

	/**
	 * 折扣券显示或者隐藏
	 */
	private void showOrHidePop () {
		bottomSheetDialog = new BottomSheetDialog(OrderConfirmActivity.this);
		View rootView = LayoutInflater.from(OrderConfirmActivity.this)
				.inflate(R.layout.layout_discount, null);
		rlDiscount = rootView.findViewById(R.id.rl_discount);
		discounts = new ArrayList<>();
		discounts.add(new DiscountBean());
		discounts.add(new DiscountBean());
		discounts.add(new DiscountBean());
		discountAdapter = new DiscountAdapter(R.layout.layout_discount_item, discounts);
		LinearLayoutManager linearLayoutManager = new LinearLayoutManager(AppUtils.getContext());
		rlDiscount.setHasFixedSize(true);
		rlDiscount.setLayoutManager(linearLayoutManager);
		rlDiscount.setAdapter(discountAdapter);

		bottomSheetDialog.setContentView(rootView);
		bottomSheetDialog.show();

	}
	@Override
	protected void onDestroy () {
		super.onDestroy();
		if (EventBus.getDefault().isRegistered(this)) {
			EventBus.getDefault().unregister(this);
		}
	}
}
